namespace DataGridHighlighterExample
{
    using System;
    using System.Data;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Collections.Generic;

    public class HighlighterConverter : IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter
        
        List<Cell> empties;
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (empties == null)
            {
                empties = new List<Cell>();
            }
            if (values[1] is DataRow)
            {
                //Change the background of any cell with 1.0 to light red.
                var cell = (DataGridCell)values[0];
                DataRow row = (DataRow)values[1];
                var columnName = cell.Column.SortMemberPath;
                System.Reflection.FieldInfo fieldInfo = row.GetType().GetField("_rowID",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 
                Int64 rowID = (Int64)fieldInfo.GetValue(row);
                Cell pom = new Cell(rowID, columnName);
                
                if (row[columnName].ToString() == "?")
                {
                    Cell cell2 = new Cell(rowID, columnName);
                    empties.Add(cell2);
                    return new SolidColorBrush(Colors.LightCoral);
                }
                else if(empties.Count>0 && empties.Contains(pom))
                {
                    return new SolidColorBrush(Colors.LimeGreen);
                }

            }
            return SystemColors.AppWorkspaceColor;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }

    public static class Extensions
    {
        public static bool IsNumeric(this object val)
        {
            double test;
            return double.TryParse(val.ToString(), out test);
        }

        public static double ToDouble(this object val)
        {
            return Convert.ToDouble(val);
        }
    }
    public class Cell : IEquatable<Cell>
    {
       Int64 row;
        string column;
        public Cell(Int64 r, string c)
        {
            row = r;
            column = c;
        }
        public bool Equals(Cell other)
        {
            if (this.row== other.row && this.column == other.column)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
}